#include "studentlogin.h"
#include "ui_studentlogin.h"
#include "ManagementSystem.h"
#include "studentloginsuccess.h"
#include <iostream>
#include <QMessageBox>

using namespace std;

StudentLogin::StudentLogin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StudentLogin)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/img/login.jpg"));
    this->sls = new StudentLoginSuccess;
    connect(sls,SIGNAL(back2login()),this,SLOT(reshow()));
}

StudentLogin::~StudentLogin()
{
    delete ui;
}

void StudentLogin::on_backmain_clicked()
{
    emit back2main();
    cout << "student login send signal" << endl;
    this->close();
}
void StudentLogin::on_submit_clicked()
{
    string username = ui->usernameLineEdit->text().toStdString();
    string password = ui->passwordLineEdit->text().toStdString();
    Student s1(username, password);
    cout << username << " " << password << endl;
    ManagementSystem* psm = ManagementSystem::GetInstance();
    bool authSuccess = psm->authStudent(&s1);
    if (authSuccess)
    {
        connect(this, SIGNAL(sendInfo(StudentInfo*)), sls, SLOT(getInfo(StudentInfo*)));
        emit sendInfo(s1.getInfo());
        this->hide();
        sls->show();
    }
    else
    {
        QMessageBox::critical(this,
               tr("登录失败"),
               tr("用户名或密码不正确"),
               QMessageBox::Ok | QMessageBox::Cancel,
               QMessageBox::Ok);
    }
}
// 重新显示当前界面
void StudentLogin::reshow()
{
    this->show();
}
